package com.gitee.elf.core.entity.impl

import com.gitee.elf.api.annotation.Id
import com.gitee.elf.api.common.MetaEntityWatcher
import com.gitee.elf.core.entity.*
import com.gitee.elf.core.entity.MetadataContainer.Companion.safeData
import com.gitee.elf.core.entity.EntityManager.getEntityInstance
import com.gitee.elf.core.data.DataBoolean
import com.gitee.elf.core.data.DataColor
import com.gitee.elf.core.data.DataDyeColor
import org.bukkit.DyeColor
import org.bukkit.entity.AnimalTamer
import org.bukkit.entity.EntityType
import org.bukkit.entity.Wolf
import org.bukkit.event.player.PlayerInteractAtEntityEvent
import org.bukkit.inventory.EquipmentSlot
import taboolib.common.platform.event.SubscribeEvent

@Id("wolf")
class ProxyEntityWolf(instance: EntityInstance) : AbstractBukkitProxyEntity<Wolf>(instance) {

    override val bukkitType = EntityType.WOLF

    val angry = MetaEntityWatcher<DataBoolean>("is-angry", this) {
        entity?.isAngry = value
    }

    val collarColor = MetaEntityWatcher<DataColor>("collar-color", this) {
        entity?.collarColor = DyeColor.getByColor(value) ?: error("DyeColor parse error: $value")
    }

    override fun onSpawn() {
        super.onSpawn()
        this.entity?.owner = instance.owner as? AnimalTamer
        this.entity?.isTamed = true
    }

    companion object {

        @SubscribeEvent
        fun e(e: PlayerInteractAtEntityEvent) {
            val entity = e.rightClicked
            if (entity is Wolf && e.hand == EquipmentSlot.OFF_HAND && !e.player.isSneaking) {
                val instance = entity.getEntityInstance() ?: return
                if (instance.owner == e.player) {
                    entity.isSitting = !entity.isSitting
                }
            }
        }

    }

}